<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item to="/home">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <el-row style="margin-bottom: 20px">
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>
      <!--      表格-->
      <tree-table :data="cateList" :columns="columns" :selection-type="false" :expand-type="false" :show-index="true"
                  index-text="#" border :show-row-hover="false">
        <!--        是否有效模板-->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-show="scope.row.cat_deleted=true" style="color: lightgreen"></i>
          <i class="el-icon-close" v-show="scope.row.cat_deleted=false" style="color: lightcoral"></i>
        </template>
        <!--        排序模板-->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini" v-show="scope.row.cat_level==0">一级</el-tag>
          <el-tag size="mini" type="success" v-show="scope.row.cat_level==1">二级</el-tag>
          <el-tag size="mini" type="warning" v-show="scope.row.cat_level==2">三级</el-tag>
        </template>
        <!--        操作模板-->
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>
      <!--      分页器-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCureentChange"
                     :current-page="queryInfo.pagenum" :page-size="queryInfo.pagesize" :page-sizes="[3,5,10,15]"
                     :total="total" layout="total,sizes,prev,pager,next,jumper"></el-pagination>
    </el-card>
    <!--    添加分类对话框-->
    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" @close="clearDialog">
      <el-form :model="addCateForm" :rules="addCateRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:" >
            <el-cascader :options="parentCateList" :props="cascadeProps" @change="parentCateChange" expand-trigger="hover" v-model="selectedKeys" clearable></el-cascader>
<!--          <el-input v-model="addCateForm.cat_name"></el-input>-->
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="addCateDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="addCate">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Cate',
  data () {
    return {
      // 分类列表
      cateList: [],
      // 总数
      total: 0,
      // 查询对象
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      // 每一列属性
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          // 表示使用插槽渲染
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          // 表示使用插槽渲染
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          // 表示使用插槽渲染
          type: 'template',
          template: 'opt'
        }
      ],
      // 添加分类对话框是否显示
      addCateDialogVisible: false,
      // 添加分类表单数据
      addCateForm: {
        // 名称
        cat_name: '',
        // 父类id
        cat_pid: 0,
        // 分类层级, 默认添加一级分类中
        cat_level: 0
      },
      // 添加分类表单验证规则
      addCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 添加分类对话框中的父级分类列表
      parentCateList: [],
      // cascader配置对象
      cascadeProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      // 被选中的keys
      selectedKeys: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    // 获取分类
    async getCateList () {
      const { data: result } = await this.$http.get('categories', { params: this.queryInfo })
      if (result.meta.status !== 200) return this.$message.error('获取商品分类失败')
      this.$message.success('获取商品分类成功')
      this.cateList = result.data.result
      this.total = result.data.total
      console.log(this.cateList)
    },
    // 监听分页变化
    handleSizeChange (newSize) {
      //
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听当前页变化
    handleCureentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getCateList()
    },
    // 显示添加分类对话框
    showAddCateDialog () {
      this.getParentCateList()
      this.addCateDialogVisible = true
    },
    // 获取父级分类列表
    async getParentCateList () {
      const { data: result } = await this.$http.get('categories', {
        params: {
          type: 2
        }
      })
      if (result.meta.status !== 200) return this.$message.error('获取父级分类列表失败')
      this.parentCateList = result.data
    },
    // 父级选择框监听事件
    parentCateChange () {
      if (this.parentCateList.length > 0) {
        // 父级id
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // 当前分类id
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        // 父级id
        this.addCateForm.cat_pid = 0
        // 当前分类id
        this.addCateForm.cat_level = 0
      }
    },
    addCate () {
      this.$refs.addCateFormRef.validate(async valid => {
        // eslint-disable-next-line no-useless-return
        if (!valid) return
        // 发起请求添加数据
        const { data: result } = await this.$http.post('categories', this.addCateForm)
        if (result.meta.status !== 201) return this.$message.error('添加失败,请重试')
        this.$message.success('添加成功')
        this.getCateList()
        this.addCateDialogVisible = false
      })
    },
    // 添加分类对话框关闭事件,重置表单数据
    clearDialog () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>

<style scoped>

</style>
